<template>
<div>
  <h1>发微博页面</h1>
  <el-input placeholder="说点儿什么..." v-model="weibo.content"></el-input>
  <el-button @click="post()">发微博</el-button>
</div>
</template>

<script>
export default {
  name: "PostView",
  data() {
    return {
      weibo:{content:""},
      user:{}
    }
  },
  methods: {
    post() {
      //把当前登录的用户id赋值给weibo对象
      this.weibo.userId = this.user.id;
      //把js对象转成特定格式的字符串
      let data = this.qs.stringify(this.weibo);
      //发出异步post请求
      this.axios.post('http://localhost:8080/v1/weibos/add-new',data)
      .then((response)=>{
        if (response.data.code==1){
          this.$message.success("发布成功!");
          this.$router.push('/');
        }else{
          this.$message.error(response.data.msg);
        }
      })
    }
  },
  mounted() {

    this.user = localStorage.user?JSON.parse(localStorage.user):null;
    //判断如果没有登录则跳转到登录页面
    if (this.user==null){
      alert("请先登录!");
      this.$router.push('/login');
    }
  }
}
</script>

<style scoped>

</style>